# Maintainer: Sergej Pupykin <pupykin.s@gmail.com>
# Contributor:Techlive Zheng <techlivezheng at gmail dot com>

pkgname=phpvirtualbox
pkgver=5.2_1
_pkgver=${pkgver/_/-}
_pkgver=${_pkgver/rc/-rc}
_commit=cf82d58f26ed82d3f7f4b5ba5e80f8754ee48c2f
pkgrel=8
pkgdesc="PHP/AJAX web interface for VirtualBox"
arch=(any)
url="https://github.com/phpvirtualbox/phpvirtualbox"
license=('GPL')
depends=('php')
makedepends=('git')
backup=("etc/webapps/phpvirtualbox/config.php")
#source=("https://github.com/phpvirtualbox/phpvirtualbox/archive/${_pkgver}.tar.gz")
source=("git+https://github.com/phpvirtualbox/phpvirtualbox.git#commit=${_commit}"
	"https://github.com/phpvirtualbox/phpvirtualbox/commit/9922d96b27db94e99b665552d3a0331b0fac90ad.patch"
	"https://github.com/phpvirtualbox/phpvirtualbox/commit/b38e821d13c59bdedab12e4b354563061dfc8d6b.patch"
	"https://github.com/phpvirtualbox/phpvirtualbox/commit/f691db99a33c05e03924027d82ddcbf9f94b0497.patch"
	"https://github.com/phpvirtualbox/phpvirtualbox/commit/c565edddca81e50d55775ae1e9c9a864c6019a3f.patch"
	"https://github.com/phpvirtualbox/phpvirtualbox/commit/f1a677166786d237a52be2ddaf161ea1937bf32f.patch")
sha256sums=('SKIP'
            '1d4c0a217765456bd94e77bf43ba72b7cb2b70ca46de767c2d33a29598845524'
            '78317f08c95ca0cf8bd1e8dacad6d2ff8904bc4c3a157d9b436a03d397774c25'
            '9ee7198358d5cf950423e4162ec90ea9d9d4d88a13b356950f877482353c1711'
            'a11d3bb325c390753da0a226365b6627c5eda0c6a86a7aae19e7b55b4cc5d955'
            '9b79401ac330606186dbb054e77d5e237c73fbe3e64b46cc16e3743dbf94696f')

prepare() {
#  cd "$srcdir"/${pkgname}-${_pkgver}
  cd "$srcdir"/${pkgname}
  for filename in "${source[@]}"; do
    if [[ "$filename" =~ \.patch$ ]]; then
      printf "Applying patch %s\n" "${filename##*/}"
      git apply -p1 <"$srcdir/${filename##*/}"
    fi
  done
}

package() {
#  cd "$srcdir"/${pkgname}-${_pkgver}
  cd "$srcdir"/${pkgname}

  mkdir -p "$pkgdir"/etc/webapps/phpvirtualbox
  install -D -m644 config.php-example "$pkgdir"/etc/webapps/phpvirtualbox/config.php

  # Apache configuration
  cat > "$pkgdir"/etc/webapps/phpvirtualbox/apache.example.conf <<EOF
  Alias /phpvirtualbox "/usr/share/webapps/phpvirtualbox"
  <Directory "/usr/share/webapps/phpvirtualbox">
    Options FollowSymlinks
    AllowOverride All
    Require all granted
  </Directory>
EOF

  find . -type f -exec install -D -m644 {,"$pkgdir"/usr/share/webapps/${pkgname}/}{} \;
  rm -r "$pkgdir"/usr/share/webapps/${pkgname}/.git
  ln -s /etc/webapps/phpvirtualbox/config.php "$pkgdir"/usr/share/webapps/phpvirtualbox/config.php
}
